#include "volumeform.h"
#include "qdebug.h"
#include "ui_volumeform.h"
#include <QGraphicsDropShadowEffect>
#include <QPainter>
volumeForm::volumeForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::volumeForm)
    ,state(true)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    ui->widget_2->installEventFilter(this);


    connect(ui->pushButton_2, &QPushButton::clicked, this, &volumeForm::on_pushButton_2_clicked);

    //自定义阴影
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setColor(0x676767);
    shadow->setBlurRadius(10);
    shadow->setOffset(0,0);
    this->setGraphicsEffect(shadow);
    ui->label->setText("50%");
    ui->pushButton_2->setStyleSheet("#pushButton_2{border-image:url(:/pic/volume.png);}"
                                    "#pushButton_2:hover{border-image:url(:/pic/volumehv.png);}");
}

void volumeForm::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    // 计算倒三角的位置
    int centerX = width() / 2;
    int bottomY = height() - 50;
    int triangleSize = 50;

    // 定义三角的三个点
    QPoint point1(centerX - triangleSize / 2, bottomY);
    QPoint point2(centerX + triangleSize / 2, bottomY);
    QPoint point3(centerX, bottomY + triangleSize / 2);

    // 绘制
    QPolygon triangle;
    triangle.append(point1);
    triangle.append(point2);
    triangle.append(point3);

    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(236, 236, 236));
    painter.drawPolygon(triangle);
}


bool volumeForm::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->widget_2)
    {
        if ( event->type() == QEvent::MouseButtonPress )
        {
            setvolume();
        }
        else if (event->type() == QEvent::MouseMove)
        {
            setvolume();
            emit setVolume(this->volume);
        }
        else if (event->type() == QEvent::MouseButtonRelease)
        {
            // 鼠标释放事件
            emit setVolume(this->volume);
        }
    }
    return QWidget::eventFilter(obj, event);
}

// 根据鼠标位置改变音量滑块位置和显示的音量百分比
void volumeForm::setvolume()
{
    int height = ui->frame->mapFromGlobal(QCursor().pos()).y();
    height = height < 10 ? 10 : height;
    height = height > 145 ? 145 : height;
    ui->pushButton->move(ui->pushButton->x(), height - ui->pushButton->height() / 2);
    ui->frame_2->setGeometry(ui->frame_2->x(), height, ui->frame_2->width(), 155 - height);

    //计算音量
    int volume = int(float(height-10)  / float(135) * 100);
    this->volume = 100 - volume;
    //显示百分比
    QString str = QString::number(this->volume,10);
    ui->label->setText( str  + "%" );
    updateVolumeIcon();
}

void volumeForm::updateVolumeIcon()
{
    if (this->volume == 0)
    {
        ui->pushButton_2->setStyleSheet(
            "#pushButton_2{border-image:url(:/pic/volumeno.png);}"
            "#pushButton_2:hover{border-image:url(:/pic/volumenohv.png);}"
            );
        state = false;
    }
    else
    {
        ui->pushButton_2->setStyleSheet(
            "#pushButton_2{border-image:url(:/pic/volume.png);}"
            "#pushButton_2:hover{border-image:url(:/pic/volumehv.png);}"
            );
        state = true;
    }
    emit getStatus(state);
}

void volumeForm::on_pushButton_2_clicked()
{
    if (state)
    {
        ui->pushButton_2->setStyleSheet("#pushButton_2{border-image:url(:/pic/volume.png);}"
                                        "#pushButton_2:hover{border-image:url(:/pic/volumehv.png);}");
        emit setVolume(0);
    }
    else
    {
        ui->pushButton_2->setStyleSheet("#pushButton_2{border-image:url(:/pic/volumeno.png);}"
                                        "#pushButton_2:hover{border-image:url(:/pic/volumenohv.png);}");
        this->volume = 0;
        emit setVolume(this->volume);
    }
    emit getStatus(state);
    state = !state;
}


void volumeForm::closeEvent(QCloseEvent *event)
{
    (void) event;
    hide();
}


volumeForm::~volumeForm()
{
    delete ui;
}
